<?php 
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
class Game extends Controller
{


	public function index(){
        $session = Session::get('admin');
    if (!empty($session)) {
    $game=Db::name("game")->select();
        //dump($activity);exit;
        $this->assign("game",$game);

        return $this->fetch();
    
}
else{
    $this->error('您没有权限!','admin/login/index');
}
		
	}
	public function delete($id){
		//先把对应的用户头像删除
		$game=Db::name("game")->find($id);
		// dump(Db::name("users")->getLastSQL());
		// dump($user);exit;
		$pic=$game["game_pic"];
		$file=ROOT_PATH."public".DS."static".DS."admin".DS."img".DS.$pic;
		if(file_exists($file))unlink($file);

		if(Db::name("game")->delete($id))
			$this->success('删除成功');
		else $this->error('删除失败');
	}
	public function create(){
 return $this->fetch('create');
    }
    public function add(){
    	$file = request()->file('game_pic');
  
    // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
        $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'. DS . 'img');
         // ;exit;
        if($info){
                $pic=$info->getFileName();
               // dump($pic);
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }

    	//获取表单数据
    	//$pname=$_POST["pname"];

        $data=[
        	'game_name'=>input('game_name'),
        	'game_pic'=>empty($pic)?"12.jpg":$pic,
        	'game_address'=>input('game_address'),
        	'game_intro'=>input('game_intro'),
        	'game_time'=>input('game_time'),
        	'game_price'=>input('game_price'),
        	'game_phone'=>input('game_phone'),
        	
        ];
    	//存入数据库
    	if(DB::name('game')->insert($data)){    		
    		//dump($data);exit;
    		$this->success('添加成功','admin/game/index');

    	}else{
    		$this->error('添加失败','admin/game/create');
    	}
    }
    public function edit(){
        //获取视频类型列表
        $game_id=input('game_id');
       // dump($game_id);
       $game=Db::name("game")->where('game_id',$game_id)->find();
        //dump($game);exit;
        $this->assign("game",$game);
        //dump($user);
        return $this->fetch('edit');
    }
  
    public function update(){

        //图片是否修改

        //获取其他的表单数据

        $game_id=input('game_id');
        //在数据库里修改
        $file = request()->file('game_pic');
        //dump($uid);
    // 移动到框架应用根目录/public/uploads/ 目录下
    if($file){
        //dump($file);
        $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'. DS . 'img');
         // ;exit;
        if($info){
                $pic=$info->getFileName();
               //dump($pic);
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
        $data=[
           'game_name'=>input('game_name'),
        	'game_pic'=>$pic,
        	'game_address'=>input('game_address'),
        	'game_intro'=>input('game_intro'),
        	'game_time'=>input('game_time'),
        	'game_price'=>input('game_price'),
        	'game_phone'=>input('game_phone'),
        ];
        //dump($activity_id);
        //dump($data);exit;
        // if(!empty($pic)){
        //  if(file_exists(ROOT_PATH . 'public' . DS . 'static'. DS . 'school'.DS.$pic))
        //      unlink(ROOT_PATH . 'public' . DS . 'static'. DS . 'school'.DS.$pic2);
        // }

        //存入数据库
        if(DB::name('game')->where('game_id',$game_id)->update($data)){            
        
            $this->success('修改成功','admin/game/index');

        }else{
            $this->error('修改失败','admin/game/edit');
        }
    }
    public function search(){
        $content=input('content');
        $game=Db::table('game')->where('game_name','like',$content)->select();
        $this->assign("game",$game);

        return $this->fetch('index');
       // dump($sql);
    }
}
?>